#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   smb_usermanager.sh
# Version:    1.0
# Date:       2013/11/20
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-04Samba-04管理samba用户
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/20
#             - smb: user, add, delete user.
# ----------------------------------------------------------------------

##! @TODO: setup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
 
    # add user on rhost
    TEST_USER="smbuser"
    TEST_USER_PASSWD="1"

    # add user
    ${RSH} -n -l root $RHOST "/usr/sbin/userdel -r ${TEST_USER}; /usr/sbin/useradd  ${TEST_USER}"

    # smbpasswd -a
    ${RSH} -n -l root $RHOST "echo -ne \"${TEST_USER_PASSWD}\n${TEST_USER_PASSWD}\n\" | /usr/bin/smbpasswd -a ${TEST_USER}"

    # check smbuser
    RE=`${RSH} -n -l root $RHOST "/usr/bin/pdbedit -L ${TEST_USER}"`
    echo ${RE} | grep "${TEST_USER}:"
    EchoResult "added user ${TEST_USER}"


    ## smbpasswd -d: disable
    RE=`${RSH} -n -l root $RHOST "/usr/bin/smbpasswd -d ${TEST_USER}"`

    # check smbuser
    echo ${RE} | grep "Disabled user ${TEST_USER}"
    EchoResult "Disabled user ${TEST_USER}"


    ## smbpasswd -e: enable
    RE=`${RSH} -n -l root $RHOST "/usr/bin/smbpasswd -e ${TEST_USER}"`

    # check smbuser
    echo ${RE} | grep "Enabled user ${TEST_USER}"
    EchoResult "Enabled user ${TEST_USER}"


    ## smbpasswd -d: delete
    ${RSH} -n -l root $RHOST "/usr/bin/smbpasswd -x ${TEST_USER}"

    # check smbuser
    RE=`${RSH} -n -l root $RHOST "/usr/bin/pdbedit -L ${TEST_USER}"`
    ! echo ${RE} | grep "${TEST_USER}:"
    EchoResult "delete user ${TEST_USER}"


}


# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}

setup_env
EchoResult "setup env"

do_test
EchoResult "testcase : ${serv} user management"
